//
//  YYT_NavigationView.m
//  AudioVisualFeast
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "QL_NavigationView.h"
#import "QL_NavigationCollectionCell.h"

@implementation QL_NavigationView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.first = 0;
        
        self.array = [[NSMutableArray alloc] initWithObjects:@"全部", @"内地", @"港台", @"欧美", @"韩国", @"日本", @"全部", @"内地", @"港台", @"欧美", @"韩国", @"日本", @"全部", @"内地", @"港台", @"欧美", @"韩国", @"日本", nil];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:_array forKey:@"23"];
        
        UICollectionViewFlowLayout *FlowLayout = [[UICollectionViewFlowLayout alloc] init];
        FlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        FlowLayout.itemSize = CGSizeMake(self.bounds.size.width / 5, self.bounds.size.height);
        FlowLayout.minimumInteritemSpacing = 0;
        FlowLayout.minimumLineSpacing = 0;
        
        
        if ((int)self.bounds.size.width % 5 == 0) {
            self.collection = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:FlowLayout];
        } else {
            int a = (int)self.bounds.size.width / 5;
            FlowLayout.itemSize = CGSizeMake(a, self.bounds.size.height);
            self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake((self.bounds.size.width - a * 5) / 2, 0, a * 5, self.bounds.size.height) collectionViewLayout:FlowLayout];
        }
        
        _collection.backgroundColor = [UIColor blackColor];
        _collection.delegate = self;
        _collection.dataSource = self;
        //        取消边缘动画效果
        _collection.bounces = NO;
        //        滑动速度
        _collection.decelerationRate = 0;
        _collection.contentOffset = CGPointMake(self.collection.frame.size.width / 5 * 10, 0);
        [_collection registerClass:[QL_NavigationCollectionCell class] forCellWithReuseIdentifier:@"NavigationCell"];
        [self addSubview:self.collection];
    }
    return self;
}
//正在滑动时
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //    向左滑
    if (scrollView.contentOffset.x == self.collection.frame.size.width / 5 * 0) {
        [scrollView setContentOffset:CGPointMake(self.collection.frame.size.width / 5 * 6, 0)];
        //        向右滑
    } else if (scrollView.contentOffset.x == self.collection.frame.size.width / 5 * 13) {
        [scrollView setContentOffset:CGPointMake(self.collection.frame.size.width / 5 * 7, 0)];
    }
    
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    QL_NavigationCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NavigationCell" forIndexPath:indexPath];
    cell.name = self.array[indexPath.item];
    if (indexPath.item == 13) {
        if (self.first != 1) {
            NSIndexPath *index = [NSIndexPath indexPathForItem:12 inSection:0];
            QL_NavigationCollectionCell *celll = (QL_NavigationCollectionCell *)[self.collection cellForItemAtIndexPath:index];
            celll.title.textColor = [UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0];
            self.first = 1;
        }
        
    }
    
    
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.array.count;
}
//cell点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    QL_NavigationCollectionCell *cell = (QL_NavigationCollectionCell *)[self.collection cellForItemAtIndexPath:indexPath];
    //    点击屏幕第一个
    if (cell.frame.origin.x == collectionView.contentOffset.x) {
        [collectionView setContentOffset:CGPointMake(collectionView.contentOffset.x - (self.collection.bounds.size.width / 5 * 2), 0) animated:YES];
        //        点击屏幕最后一个
    } else if (cell.frame.origin.x - collectionView.contentOffset.x == self.collection.frame.size.width / 5 * 4) {
        [collectionView setContentOffset:CGPointMake(collectionView.contentOffset.x + (self.collection.bounds.size.width / 5 * 2), 0) animated:YES];
        //        点击屏幕第二个
    } else if (cell.frame.origin.x - collectionView.contentOffset.x == self.collection.frame.size.width / 5) {
        [collectionView setContentOffset:CGPointMake(collectionView.contentOffset.x - self.collection.bounds.size.width / 5, 0) animated:YES];
        //        点击屏幕倒数第二个
    } else if (cell.frame.origin.x - collectionView.contentOffset.x == self.collection.frame.size.width / 5 * 3) {
        [collectionView setContentOffset:CGPointMake(collectionView.contentOffset.x + self.collection.bounds.size.width / 5, 0) animated:YES];
    }
    
}


//滑动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //    滑动结束时计算  当前偏移量／按钮长度，整除说明位置正好
    if ((NSInteger)scrollView.contentOffset.x % (NSInteger)(self.collection.frame.size.width / 5) == 0) {
        [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x, 0)];
        //    滑动结束时计算  当前偏移量／按钮长度，现在偏移量的位置大于item长度的一半，偏移量＋一个item的长度
    } else if(scrollView.contentOffset.x / (self.collection.frame.size.width / 5) - (NSInteger)(scrollView.contentOffset.x / (self.collection.frame.size.width / 5)) > 0.5) {
        [scrollView setContentOffset:CGPointMake((NSInteger)(scrollView.contentOffset.x / (self.collection.frame.size.width / 5)) * (self.collection.frame.size.width / 5) + (self.collection.frame.size.width / 5), 0)  animated:YES];
        //    滑动结束时计算  当前偏移量／按钮长度，现在偏移量的位置小于item长度的一半，偏移量修正
    } else if(scrollView.contentOffset.x / (self.collection.frame.size.width / 5) - (NSInteger)(scrollView.contentOffset.x / (self.collection.frame.size.width / 5)) < 0.5) {
        [scrollView setContentOffset:CGPointMake((NSInteger)(scrollView.contentOffset.x / (self.collection.frame.size.width / 5)) * (self.collection.frame.size.width / 5), 0)  animated:YES];
    }
}
//动画结束
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSInteger num = scrollView.contentOffset.x / (self.collection.frame.size.width / 5) + 2;
    NSArray *array = [self.collection subviews];
    for (int i = 0; i < 6; i++) {
        QL_NavigationCollectionCell *celll = array[i];
        celll.title.textColor = [UIColor whiteColor];
    }
    
    NSIndexPath *index = [NSIndexPath indexPathForItem:num inSection:0];
    QL_NavigationCollectionCell*cell = (QL_NavigationCollectionCell *)[self.collection cellForItemAtIndexPath:index];
    cell.title.textColor = [UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0];
    if (num == 9) {
        NSIndexPath *index = [NSIndexPath indexPathForItem:16 inSection:0];
        QL_NavigationCollectionCell *cell = (QL_NavigationCollectionCell *)[self.collection cellForItemAtIndexPath:index];
        cell.title.textColor = [UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0];
    }
    if (num == 8) {
        NSIndexPath *index = [NSIndexPath indexPathForItem:14 inSection:0];
        QL_NavigationCollectionCell *cell = (QL_NavigationCollectionCell *)[self.collection cellForItemAtIndexPath:index];
        cell.title.textColor = [UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0];
    }
    [self.delegate cellClick:num];
}

//拖拽
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    //    滑动结束时计算  当前偏移量／按钮长度，整除说明位置正好
    if ((NSInteger)scrollView.contentOffset.x % (NSInteger)(self.collection.frame.size.width / 5) == 0) {
        [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x, 0)];
        //    滑动结束时计算  当前偏移量／按钮长度，现在偏移量的位置大于item长度的一半，偏移量＋一个item的长度
    } else if(scrollView.contentOffset.x / (self.collection.frame.size.width / 5) - (NSInteger)(scrollView.contentOffset.x / (self.collection.frame.size.width / 5)) > 0.5) {
        [scrollView setContentOffset:CGPointMake((NSInteger)(scrollView.contentOffset.x / (self.collection.frame.size.width / 5)) * (self.collection.frame.size.width / 5) + (self.collection.frame.size.width / 5), 0)  animated:YES];
        NSInteger num = scrollView.contentOffset.x / (self.collection.frame.size.width / 5) + 3;
        [self.delegate cellClick:num];
        //    滑动结束时计算  当前偏移量／按钮长度，现在偏移量的位置小于item长度的一半，偏移量修正
    } else if(scrollView.contentOffset.x / (self.collection.frame.size.width / 5) - (NSInteger)(scrollView.contentOffset.x / (self.collection.frame.size.width / 5)) < 0.5) {
        [scrollView setContentOffset:CGPointMake((NSInteger)(scrollView.contentOffset.x / (self.collection.frame.size.width / 5)) * (self.collection.frame.size.width / 5), 0)  animated:YES];
        NSInteger num = scrollView.contentOffset.x / (self.collection.frame.size.width / 5) + 2;
        [self.delegate cellClick:num];
    }
}


@end
